package course.dmsSpringBoot.controller;

import course.dmsSpringBoot.entity.Administrator;
import course.dmsSpringBoot.entity.ResultMap;
import course.dmsSpringBoot.service.AdministratorService;
import course.dmsSpringBoot.utils.CheckString;
import org.apache.ibatis.annotations.Param;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员
 * @author zouran
 * createDate：2023/12/18  21:31
 */
@RestController
@RequestMapping("/admin")
public class AdministratorController {
    private final ResultMap<Object> resultMap;
    private final AdministratorService administratorService;

    public AdministratorController(ResultMap<Object> resultMap, AdministratorService administratorService) {
        this.resultMap = resultMap;
        this.administratorService = administratorService;
    }
    @PostMapping("/logIn")
    public ResultMap<String> checkLogIn(@Param("email") String email, @Param("password") String password) {
        String message="";
        int code=201;
        if (!administratorService.checkLogIn(email,password))
            message="账号或密码错误!";
        else {
            message = "登录成功!";
            code=200;
        }
        return resultMap.codeAndMessage(code,message);
    }
    @PostMapping("/changePassword")
    public ResultMap<String> changePassword(@Param("email")String email, @Param("oldPassword")String oldPassword, @Param("newPassword")String newPassword) {
        String message="";
        int code=201;
        if (!administratorService.changePassword(email,oldPassword,newPassword))
            message="账号或密码错误!";
        else {
            code=200;
            message = "修改密码成功!";
        }
        return resultMap.codeAndMessage(code,message);
    }
    @PostMapping("/register")
    public ResultMap<String> register(@NotNull @RequestBody Administrator administrator){
        String message="";
        int code=201;
        if (!CheckString.isEmailValid(administrator.getEmail()))
            message="邮箱格式错误!";
        if (!CheckString.isNonChineseString(administrator.getEmail()))
            message="密码格式错误!";
        if (!administratorService.register(administrator))
            message="账号已存在";
        else {
            code=200;
            message = "注册成功!";
        }
        return resultMap.codeAndMessage(code,message);

    }
}
